# 高阶函数：以函数作为参数或者以函数作为返回值的函数
# 函数是Python的一等公民
# 知识点
# x = 1
# y = x
#
# def test(a):
#     print(a)
#
#     z = 1000
#     return z
#
#
# test(10)
# test(x)

# 高级函数的使用

# 版本1
# def foo():
#     print("start")
#     print("foo")
#     print("end")
#
#
# def bar():
#     print("start")
#     print("bar")
#     print("end")


# 版本2
# def foo():
#     print("foo")
#
#
# def bar():
#     print("bar")
#
#
# def dec(f):
#     print("start")
#     f()
#     print("end")
#
# # foo()
# dec(foo)
# dec(bar)


# (15) filter

# l = [23, 4, 5, 67, 86, 21]

# print([i for i in l if i % 2 == 0])
# 版本1
# def get_even(item):
#     # 方式1
#     # if item % 2 == 0:
#     #     return True
#     # else:
#     #     return False
#     return item % 2 != 0
#
# print(list(filter(get_even, l)))

# 版本2

# print(list(filter(lambda item: item % 2 == 0, l)))


# (16) map函数

# l = [1, 2, 3, 4, 5, 6]
#
# # print([i * i for i in l])
#
# print(list(map(lambda item: item * item, l)))

# (17) sorted
# l = [34, 5, 6, 12, 41]
# l.sort()
# print(l)

# 案例1
# data01 = [("yuan", 18), ('alex', 45), ("peiQi", 32)]

# data01.sort()
# print(data01)

# 方式1
# def my_order(item):
#     return item[1]
#
#
# print(sorted(data01, key=my_order,reverse=True))

# 方式2
# print(sorted(data01, key=lambda item: item[1], reverse=True))


# 案例2
# data02 = [
#     {"name": "yuan", "age": 18, "height": 192},
#     {"name": "alex", "age": 48, "height": 162},
#     {"name": "peiQi", "age": 38, "height": 172},
#     {"name": "rain", "age": 58, "height": 182},
# ]
#
# # 按年龄排序
# print(list(sorted(data02, key=lambda i: i["age"])))
# # 按身高排序
# print(list(sorted(data02, key=lambda i: i["height"])))



